/**aixos封装模块 */
import axios from "axios"
import { getSessionItem } from "./storage"
import router from "../router/index"
import { ElMessage } from "element-plus"

const instance = axios.create({
    baseURL: "http://localhost:3000",
    timeout: 60000
})
instance.interceptors.request.use(request => {
    // 请求拦截器
    let token = getSessionItem("token")
    if (token) {
        // 添加请求头
        request.headers["Authorization"] = token
    }
    return request
})
instance.interceptors.response.use(response => {
    // 响应拦截器
    if (response.data.code === 405) {
        // 用户未登录或者令牌过期
        ElMessage({
            type: "warning",
            message: "用户未登录"
        })
        router.push({ name: "login" })
    }
    return response.data
})
function request({
    url, method = "get", params = {}, data = {}, headers = {}, controller
}) {
    const signal = controller ? controller.signal : undefined
    return instance({
        url, method, params, data, headers, controller, signal
    })
}
export default request